# -*- coding: utf-8 -*-

import csv

class Row(object):

    def get_distance(self):
        return self.__distance

    def get_sight(self):
        return self.__sight

    def get_fork(self):
        return self.__fork

    def get_d_xthou(self):
        return self.__dXthou

    def get_vd(self):
        return self.__Vd

    def get_vv(self):
        return self.__Vv

    def get_vb(self):
        return self.__Vb

    def get_z(self):
        return self.__Z

    def get_d_zw(self):
        return self.__dZw

    def get_d_xw(self):
        return self.__dXw

    def get_d_xh(self):
        return self.__dXh

    def get_d_xt(self):
        return self.__dXt

    def get_d_xtz(self):
        return self.__dXtz

    def get_d_xv_0(self):
        return self.__dXV0

    def get_d_xq(self):
        return self.__dXq

    def get_alpha(self):
        return self.__alpha

    def get_theta(self):
        return self.__theta

    def get_vc(self):
        return self.__Vc

    def get_tc(self):
        return self.__tc

    def get_ybull(self):
        return self.__Ybull

    def get_ys(self):
        return self.__Ys

    def set_distance(self, value):
        self.__distance = value

    def set_sight(self, value):
        self.__sight = value

    def set_fork(self, value):
        self.__fork = value

    def set_d_xthou(self, value):
        self.__dXthou = value

    def set_vd(self, value):
        self.__Vd = value

    def set_vv(self, value):
        self.__Vv = value

    def set_vb(self, value):
        self.__Vb = value

    def set_z(self, value):
        self.__Z = value

    def set_d_zw(self, value):
        self.__dZw = value

    def set_d_xw(self, value):
        self.__dXw = value

    def set_d_xh(self, value):
        self.__dXh = value

    def set_d_xt(self, value):
        self.__dXt = value

    def set_d_xtz(self, value):
        self.__dXtz = value

    def set_d_xv_0(self, value):
        self.__dXV0 = value

    def set_d_xq(self, value):
        self.__dXq = value

    def set_alpha(self, value):
        self.__alpha = value

    def set_theta(self, value):
        self.__theta = value

    def set_vc(self, value):
        self.__Vc = value

    def set_tc(self, value):
        self.__tc = value

    def set_ybull(self, value):
        self.__Ybull = value

    def set_ys(self, value):
        self.__Ys = value

    def del_distance(self):
        del self.__distance

    def del_sight(self):
        del self.__sight

    def del_fork(self):
        del self.__fork

    def del_d_xthou(self):
        del self.__dXthou

    def del_vd(self):
        del self.__Vd

    def del_vv(self):
        del self.__Vv

    def del_vb(self):
        del self.__Vb

    def del_z(self):
        del self.__Z

    def del_d_zw(self):
        del self.__dZw

    def del_d_xw(self):
        del self.__dXw

    def del_d_xh(self):
        del self.__dXh

    def del_d_xt(self):
        del self.__dXt

    def del_d_xtz(self):
        del self.__dXtz

    def del_d_xv_0(self):
        del self.__dXV0

    def del_d_xq(self):
        del self.__dXq

    def del_alpha(self):
        del self.__alpha

    def del_theta(self):
        del self.__theta

    def del_vc(self):
        del self.__Vc

    def del_tc(self):
        del self.__tc

    def del_ybull(self):
        del self.__Ybull

    def del_ys(self):
        del self.__Ys

    distance = property(get_distance, set_distance, del_distance, "distance's docstring")
    sight = property(get_sight, set_sight, del_sight, "sight's docstring")
    fork = property(get_fork, set_fork, del_fork, "fork's docstring")
    dXthou = property(get_d_xthou, set_d_xthou, del_d_xthou, "dXthou's docstring")
    Vd = property(get_vd, set_vd, del_vd, "Vd's docstring")
    Vv = property(get_vv, set_vv, del_vv, "Vv's docstring")
    Vb = property(get_vb, set_vb, del_vb, "Vb's docstring")
    Z = property(get_z, set_z, del_z, "Z's docstring")
    dZw = property(get_d_zw, set_d_zw, del_d_zw, "dZw's docstring")
    dXw = property(get_d_xw, set_d_xw, del_d_xw, "dXw's docstring")
    dXh = property(get_d_xh, set_d_xh, del_d_xh, "dXh's docstring")
    dXt = property(get_d_xt, set_d_xt, del_d_xt, "dXt's docstring")
    dXtz = property(get_d_xtz, set_d_xtz, del_d_xtz, "dXtz's docstring")
    dXV0 = property(get_d_xv_0, set_d_xv_0, del_d_xv_0, "dXV0's docstring")
    dXq = property(get_d_xq, set_d_xq, del_d_xq, "dXq's docstring")
    alpha = property(get_alpha, set_alpha, del_alpha, "alpha's docstring")
    theta = property(get_theta, set_theta, del_theta, "theta's docstring")
    Vc = property(get_vc, set_vc, del_vc, "Vc's docstring")
    tc = property(get_tc, set_tc, del_tc, "tc's docstring")
    Ybull = property(get_ybull, set_ybull, del_ybull, "Ybull's docstring")
    Ys = property(get_ys, set_ys, del_ys, "Ys's docstring")

def mapfunc(x):
    x = x.strip()
    if x == '':
        return 0.0
    else:
        return float(x.replace(',', '.'))
   
class Table(object):
    
    def searchDistance(self, distance):
        if distance < min(self.rows)[0]:
            return min(self.rows)[1]
        elif distance > max(self.rows)[0]:
            return max(self.rows)[1]
        else:
            for row in self.rows:
                if row[0] > distance:
                    i = self.rows.index(row) - 1
                    return self.rows[i][1]
                
    
    def __init__(self, filename):
        csvtable = csv.reader(open(filename, 'rb'), delimiter=';')
        
        self.rows = list()
        self.filename = filename
        
        # skip first 2 lines
        next(csvtable)
        next(csvtable)
        
        for csvrow in csvtable:
            maprow = map(mapfunc, csvrow)
            
            row = Row()
            row.distance = maprow[0]
            row.sight = maprow[3]
            row.fork = maprow[4]
            row.dXthou = maprow[5]
            row.Vd = maprow[6]
            row.Vv = maprow[7]
            row.Vb = maprow[8]
            row.Z = maprow[9]
            row.dZw = maprow[10]
            row.dXw = maprow[11]
            row.dXh = maprow[12]
            row.dXt = maprow[13]
            row.dXtz = maprow[14]
            row.dXV0 = maprow[15]
            row.dXq = maprow[16]
            row.alpha = maprow[17]
            row.theta = maprow[18]
            row.Vc = maprow[19]
            row.tc = maprow[20]
            row.Ybull = maprow[21]
            row.Ys = maprow[22]
            
            self.rows.append((maprow[0], row))

if __name__ == '__main__':
    tableDir = '../../tables/'
    
    first = Table(tableDir + 'first.csv')
    second = Table(tableDir + 'second.csv')
    third = Table(tableDir + 'third.csv')
    full = Table(tableDir + 'full.csv')
    reduced = Table(tableDir + 'reduced.csv')
    
    
